package Util;

import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;


@Component
public class SignEncrypt {

    public static final String BC_PROV_ALGORITHM_SHA1RSA = "SHA1withRSA";

    private static final int MAX_ENCRYPT_BLOCK = 117;

    //private static final int MAX_DECRYPT_BLOCK = 128;
    private static final int MAX_DECRYPT_BLOCK = 256;

    public boolean verify(String data, String sign, PublicKey publicKey) {
        try {
            Signature signature = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA);
            signature.initVerify(publicKey);
            signature.update(data.getBytes(Constants.UTF));
            return signature.verify(org.bouncycastle.util.encoders.Base64.decode(sign.getBytes()));
        } catch (Exception e) {
            return false;
        }
    }


    public String sign(String obj, PrivateKey privateKey) {
        String sign = "";
        try {
            Signature signature = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA);
            signature.initSign(privateKey);
            signature.update(obj.getBytes(Constants.UTF));
            sign = new String(org.bouncycastle.util.encoders.Base64.encode(signature.sign()));
        } catch (Exception e) {
        }
        return sign;
    }

    public String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        int inputLen = data.getBytes().length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offset = 0;
        byte[] cache;
        int i = 0;
        while (inputLen - offset > 0) {
            if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
            offset = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return new String(org.bouncycastle.util.encoders.Base64.encode(encryptedData), Constants.UTF);
    }

    public String decrypt(String data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] dataBytes = org.bouncycastle.util.encoders.Base64.decode(data.getBytes());
        int inputLen = dataBytes.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offset = 0;
        byte[] cache;
        int i = 0;
        while (inputLen - offset > 0) {
            if (inputLen - offset > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
            offset = i * MAX_DECRYPT_BLOCK;
        }
        out.close();
        return out.toString(Constants.UTF);
    }


    public static class Constants {

        public static final String UTF = "utf-8";
    }

}
